package com.nexgo.payment.trans.setting.transmanage;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.nexgo.payment.R;
import com.nexgo.payment.constrant.PreferencesConstants;
import com.nexgo.payment.constrant.SysConstant;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.trans.setting.SaveParameterActivity;
import com.nexgo.payment.trans.setting.SaveParameterPresenter;
import com.nexgo.payment.util.StringUtils;
import com.nexgo.payment.util.UIUtils;
import com.nexgo.payment.widget.AmountTextWatcher;
import com.nexgo.payment.widget.SwitchButton;
import com.nexgo.payment.widget.TitleBuilder;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 非接参数控制
 */
public class SetupRfParameterActivity extends SaveParameterActivity {

    @BindView(R.id.qps_support_free_password)
    SwitchButton qpsSupportFreePassword;
    @BindView(R.id.qps_limit_of_free_password)
    EditText qpsLimitOfFreePassword;
    @BindView(R.id.qps_support_free_signature)
    SwitchButton qpsSupportFreeSignature;
    @BindView(R.id.qps_limit_of_free_signature)
    EditText qpsLimitOfFreeSignature;
    @BindView(R.id.priority_to_use_rf)
    SwitchButton priorityToUseRf;
    @BindView(R.id.qps_support_card_bin_a)
    SwitchButton qpsSupportCardBinA;
    @BindView(R.id.qps_support_card_bin_b)
    SwitchButton qpsSupportCardBinB;
    @BindView(R.id.qps_support_cdcvm)
    SwitchButton qpsSupportCdcvm;
    @BindView(R.id.ec_free_signature)
    SwitchButton ecFreeSignature;
    @BindView(R.id.rf_force_online)
    SwitchButton rfForceOnline;
    @BindView(R.id.qps_support_card_bin_c)
    SwitchButton qpsSupportCardBinC;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup_rf_parameter);
        ButterKnife.bind(this);
        new SaveParameterPresenter(this, Repository.getInstance());

        initTitle();
    }

    //初始化标题栏
    private void initTitle() {
        new TitleBuilder(this)
                .setExternalTitleBgRes(R.drawable.bg_blue_icon)
                .setLeftImage(R.drawable.arrow_icon)
                .setRightText(getString(R.string.action_save))
                .setTitleText(getString(R.string.rf_parameter_settings))
                .build();
    }

    @Override
    protected HashMap<String, String> getDataBind() {
        HashMap<String, String> map = new HashMap<>();
        map.put(PreferencesConstants.QPS_SUPPORT_FREE_PASSWORD, qpsSupportFreePassword.isChecked() ? "1" : "0");
        String amount = StringUtils.getFormatAmount(qpsLimitOfFreePassword.getText().toString());
        map.put(PreferencesConstants.QPS_LIMIT_OF_FREE_PASSWORD, amount);
        map.put(PreferencesConstants.QPS_SUPPORT_FREE_SIGNATURE, qpsSupportFreeSignature.isChecked() ? "1" : "0");
        amount = StringUtils.getFormatAmount(qpsLimitOfFreeSignature.getText().toString());
        map.put(PreferencesConstants.QPS_LIMIT_OF_FREE_SIGNATURE, amount);
        map.put(PreferencesConstants.PRIORITY_TO_USE_RF, priorityToUseRf.isChecked() ? "1" : "0");
        map.put(PreferencesConstants.RF_PRIORITY_ONLINE, rfForceOnline.isChecked() ? "1" : "0");
        map.put(PreferencesConstants.QPS_SUPPORT_CARD_BIN_A, qpsSupportCardBinA.isChecked() ? "1" : "0");
        map.put(PreferencesConstants.QPS_SUPPORT_CARD_BIN_B, qpsSupportCardBinB.isChecked() ? "1" : "0");
        map.put(PreferencesConstants.QPS_SUPPORT_CARD_BIN_C, qpsSupportCardBinC.isChecked() ? "1" : "0");
        map.put(PreferencesConstants.QPS_SUPPORT_CDCVM, qpsSupportCdcvm.isChecked() ? "1" : "0");
        map.put(PreferencesConstants.EC_FREE_SIGNATURE, ecFreeSignature.isChecked() ? "1" : "0");
        return map;
    }

    @Override
    public void showDetail() {
        qpsSupportFreePassword.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.QPS_SUPPORT_FREE_PASSWORD)));
        qpsLimitOfFreePassword.setText(StringUtils.formatAmount(mPresenter.getParamValue(PreferencesConstants.QPS_LIMIT_OF_FREE_PASSWORD), SysConstant.FUND_DIGITS));
        qpsSupportFreeSignature.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.QPS_SUPPORT_FREE_SIGNATURE)));
        qpsLimitOfFreeSignature.setText(StringUtils.formatAmount(mPresenter.getParamValue(PreferencesConstants.QPS_LIMIT_OF_FREE_SIGNATURE), SysConstant.FUND_DIGITS));
        priorityToUseRf.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.PRIORITY_TO_USE_RF)));
        rfForceOnline.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.RF_PRIORITY_ONLINE)));
        qpsSupportCardBinA.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.QPS_SUPPORT_CARD_BIN_A)));
        qpsSupportCardBinB.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.QPS_SUPPORT_CARD_BIN_B)));
        qpsSupportCardBinC.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.QPS_SUPPORT_CARD_BIN_C)));
        qpsSupportCdcvm.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.QPS_SUPPORT_CDCVM)));
        ecFreeSignature.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.EC_FREE_SIGNATURE)));

        qpsLimitOfFreePassword.addTextChangedListener(new AmountTextWatcher(qpsLimitOfFreePassword));
        qpsLimitOfFreeSignature.addTextChangedListener(new AmountTextWatcher(qpsLimitOfFreeSignature));
    }

    @OnClick({R.id.title_iv_left, R.id.title_tv_right})
    public void onClicked(View view) {
        if (UIUtils.isDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.title_iv_left:
                finish();
                break;
            case R.id.title_tv_right:
                if (mPresenter != null) {
                    mPresenter.saveParams(getDataBind());
                }
                break;
            default:
                break;
        }
    }

    public static Intent newInstance(Context context) {
        return new Intent(context, SetupRfParameterActivity.class);
    }
}
